package com.data.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.config.dao.ChargeConfigDao;
import com.config.dao.EquipmentDao;
import com.config.dao.LineConfigDao;
import com.config.entity.ChargeConfig;
import com.config.entity.Equipment;
import com.config.entity.LineConfig;
import com.data.dao.ChargeDayDao;
import com.data.dao.ChargeWeekDao;
import com.data.entity.ChargeDay;
import com.data.entity.ChargeWeek;

@Service("chargeWeekService")
public class ChargeWeekServiceImpl implements ChargeWeekService{
	@Resource(name="lineConfigDao")
	private LineConfigDao lineConfigDao;
	@Resource(name="equipmentDao")
	private EquipmentDao equipmentDao;
	@Resource(name="chargeWeekDao")
	private ChargeWeekDao chargeWeekDao;
	@Resource(name="chargeDayDao")
	private ChargeDayDao chargeDayDao;
	@Resource(name="chargeConfigDao")
	private ChargeConfigDao chargeConfigDao;
	/**
	 * 根据时间保存电费周记录
	 * @param belongDate
	 */
	@Override
	public void saveChargeWeek(Date belongDate) {
		List<LineConfig> lines=lineConfigDao.findAllLine();
		if(lines.size()!=0){
			for(LineConfig line:lines){
			//	this.saveLineChargeWeek(line.getId(), belongDate);  生产线不提取数据
				List<Equipment> equs=equipmentDao.listByLine(line.getId());
				if(equs.size()!=0){
					for(Equipment e:equs){
						this.saveEquChargeWeek(e.getId(), belongDate);
					}
				}
			}
		}
	}
	/**
	 * 提取生产线周电费数据
	 * @param lineId
	 * @param belongDate
	 */
//	private void saveLineChargeWeek(int lineId,Date belongDate){
//		SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd");
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
//		Calendar ca=Calendar.getInstance();
//		ca.setTime(belongDate);
//		int belongWeek=ca.get(Calendar.WEEK_OF_YEAR);
//		int belongYear=Integer.parseInt(sdf.format(belongDate));
//		List<ChargeConfig>configs=chargeConfigDao.findDistinctChargesName();
//		if(configs.size()!=0){
//			for(ChargeConfig config:configs){
//				ChargeWeek week=chargeWeekDao.getChargeWeekByDate(lineId, belongYear, belongWeek,config.getChargeName(),"Y");
//				boolean isNew=false;
//				if(week==null){
//					week=new ChargeWeek();
//					week.setLineId(lineId);
//                    week.setBelongYear(belongYear);
//                    week.setBelongWeek(belongWeek);
//                    week.setChargeName(config.getChargeName());
//					isNew=true;
//				}
//				Calendar  cal=Calendar.getInstance();
//				cal.set(Calendar.YEAR, belongYear);//设置年份
//				cal.set(Calendar.WEEK_OF_YEAR,belongWeek);//设置第几周
//				cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);//设置周一为第一天
//				String startDate=sdft.format(cal.getTime());//某一周的开始日期
//				String endDate=sdft.format(belongDate);
//				//从开始日期到结束日期的每天电费记录
//				List<ChargeDay> days=chargeDayDao.getChargeDaysByDate(lineId, startDate,endDate,config.getChargeName(),"Y");
//				double relativeComp=0.0;double charges=0.0;
//				if(days.size()!=0){
//					for(ChargeDay day:days){
//						relativeComp+=day.getRelativeComp();
//						charges+=day.getCharge();
//					}
//		        }
//				week.setRelativeComp(relativeComp);
//				week.setCharge(charges);
//				if(isNew){
//					chargeWeekDao.save(week);
//				}else{
//					chargeWeekDao.update(week);
//				}
//			}
//		}
//	}
	
	/**
	 * 提取设备周电费数据
	 * @param lineId
	 * @param belongDate
	 */
	private void saveEquChargeWeek(int equId,Date belongDate){
		SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Calendar ca=Calendar.getInstance();
		ca.setTime(belongDate);
		int belongWeek=ca.get(Calendar.WEEK_OF_YEAR);
		int belongYear=Integer.parseInt(sdf.format(belongDate));
		List<ChargeConfig>configs=chargeConfigDao.findDistinctChargesName();
		if(configs.size()!=0){
			for(ChargeConfig config:configs){
				ChargeWeek week=chargeWeekDao.getChargeWeekByDate(equId, belongYear, belongWeek,config.getChargeName(),"N");
				boolean isNew=false;
				if(week==null){
					week=new ChargeWeek();
					week.setEquId(equId);
                    week.setBelongYear(belongYear);
                    week.setBelongWeek(belongWeek);
                    week.setChargeName(config.getChargeName());
					isNew=true;
				}
				Calendar  cal=Calendar.getInstance();
				cal.set(Calendar.YEAR, belongYear);//设置年份
				cal.set(Calendar.WEEK_OF_YEAR,belongWeek);//设置第几周
				cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);//设置周一为第一天
				String startDate=sdft.format(cal.getTime());//某一周的开始日期
				String endDate=sdft.format(belongDate);
				//从开始日期到结束日期的每天电费记录
				List<ChargeDay> days=chargeDayDao.getChargeDaysByDate(equId, startDate,endDate,config.getChargeName(),"N");
				double relativeComp=0.0;double charges=0.0;
				if(days.size()!=0){
					for(ChargeDay day:days){
						relativeComp+=day.getRelativeComp();
						charges+=day.getCharge();
					}
		        }
				week.setRelativeComp(relativeComp);
				week.setCharge(charges);
				if(isNew){
					chargeWeekDao.save(week);
				}else{
					chargeWeekDao.update(week);
				}
			}
		}
	}
}
